<template>
  <div class="container">
    <div style="width: 100%; padding: 20px">
     <div class="row">
       <Button   style="width: 50px;height: 36px;margin-right: 20px" class="ivu-btn" type="primary" @click="send">发送</Button>
       <Input v-model="input" placeholder="搜索题材、映射" icon="ios-search" @on-click="clickSearch" size="large"
              style="margin-bottom: 10px" @on-keyup="doSearch"/>

     </div>

      <div class="row between" style="width: 100%">
        <div style="width: 49%">
          <span style="font-size: 16px">公告</span>
          <Table height="750" :columns="noticeHeader" :data="noticeData" border highlight-row
                 @on-current-change="onSelected" style="margin-top: 10px;margin-bottom: 10px"></Table>
        </div>
        <div style="width: 49%">
          <span style="font-size: 16px">问答</span>
          <Table height="750" :columns="questionHeader" :data="questionData" border highlight-row
                 @on-current-change="onSelected" style="margin-top: 10px;"></Table>
        </div>
      </div>


    </div>
  </div>
</template>

<script>

  import axios from '../../src/assets/http/http'

  export default {
    name: 'Announcement',
    data() {
      return {
        input: '',
        noticeData: [],
        questionData: [],
        noticeHeader: [
          {
            title: 'id',
            key: 'id',
            minWidth: 15,
            align: 'center',

          }, {
            title: '摘要',
            key: 'abs',
            minWidth: 425,
            align: 'center',
            render: (h, params) => {
              return h('div', [
                h('div', {
                  domProps: {
                    innerHTML:params.row.abs
                  },

                }, ""),
              ])
            }
          },
          {
            title: '股票名称',
            key: 'stockCode',
            minWidth: 40,
            align: 'center',

          },

          {
            title: '公告时间',
            key: 'siteTime',
            minWidth: 50,
            align: 'center',
            sortable: true
          },
          {
            title: '公告链接',
            key: 'adjunctUrl',
            minWidth: 125,
            align: 'center',
            render: (h, params) => {
              return h('div', [
                h('a', {
                  props: {
                    type: 'success',
                    size: 'small'
                  },

                  on: {
                    click: () => {
                      window.open(params.row.adjunctUrl)
                    }
                  }
                }, params.row.adjunctUrl),


              ]);
            }

          },
        ],
        questionHeader: [
          {
            title: '股票名称',
            key: 'code',
            minWidth: 30,
            align: 'center',

          },
          {
            title: '问题',
            key: 'question',
            minWidth: 250,
            align: 'center',
            render: (h, params) => {
              return h('div', [
                h('div', {
                  domProps: {
                    innerHTML:params.row.question
                  },

                }, ""),
              ])
            }
          },
          {
            title: '问题时间',
            key: 'questionTime',
            minWidth: 50,
            align: 'center',
            sortable: true
          },
          {
            title: '回答',
            key: 'answer',
            minWidth: 250,
            align: 'center',
            render: (h, params) => {
              return h('div', [
                h('div', {
                  domProps: {
                    innerHTML:params.row.answer
                  },

                }, ""),
              ])
            }
          },
          {
            title: '回答时间',
            key: 'answerTime',
            minWidth: 50,
            align: 'center',
            sortable: true
          },
        ],
      }
    },

    methods: {
      send(){
        axios.get("man/keyword/addKeyword?keyword="+ this.input).then(rsp=>{
          if(rsp.data.retCode ===1){
            this.$Message.success("成功发送");
          }
        }).catch(error=>{
          console.log(error);
        })
      },
      clickSearch() {
        if (this.input === undefined || this.input.trim() === '') {
          return;
        }
        this.getNoticeData();
        this.getQuestionData();
      },
      doSearch(event) {
        if (this.input === undefined || this.input.trim() === '') {
          return;
        }

        if (event.keyCode === 13) {
          this.getNoticeData();
          this.getQuestionData();
        }

      },
      getNoticeData() {
        axios.get('search/notice?keyword=' + this.input).then(rsp => {
          if (rsp.data.retCode === 1) {
            if (rsp.data.retData) {
              this.noticeData = rsp.data.retData;
              var stocks = '';
              for (var i = 0; i < this.noticeData.length; i++) {
                this.noticeData[i].crawingTime = this.noticeData[i].crawingTime;
                this.noticeData[i].siteTime = this.noticeData[i].siteTime;
                stocks += this.noticeData[i].stockCode.substring(0, 6) + ',';
              }
              this.getStockMap(stocks);
            } else {
              this.noticeData = [];
            }
          }
        })

      },
      getQuestionData() {
        axios.get('search/questionAnswer?keyword=' + this.input).then(rsp => {
          if (rsp.data.retCode === 1) {
            if (rsp.data.retData) {
              this.questionData = rsp.data.retData;
              var stocks = '';
              for (var i = 0; i < this.questionData.length; i++) {
                this.questionData[i].questionTime = this.questionData[i].questionTime;
                this.questionData[i].answerTime =this.questionData[i].answerTime;
                stocks += this.questionData[i].code.substring(0, 6) + ',';
              }
              this.getStockMapCode(stocks);
            } else {
              this.questionData = [];
            }
          }
        })
      },
      getStockMap(stocks) {
        axios.get('stock/getStockName?stockCode=' + stocks).then(rsp => {
          if (rsp.data.retCode === 1) {
            if (rsp.data.retData) {
              var map = rsp.data.retData;
              var list = this.noticeData;
              for (var i = 0; i < list.length; i++) {
                list[i].stockCode = map[list[i].stockCode.substring(0, 6)];
              }
            }
          }
        }).catch(error => {
          console.log(error);
        })
      },
      getStockMapCode(stocks) {
        axios.get('stock/getStockName?stockCode=' + stocks).then(rsp => {
          if (rsp.data.retCode === 1) {
            if (rsp.data.retData) {
              var map = rsp.data.retData;
              var list = this.questionData;
              for (var i = 0; i < list.length; i++) {
                list[i].code = map[list[i].code.substring(0, 6)];
              }
            }
          }
        }).catch(error => {
          console.log(error);
        })
      },

    },
    created() {

    }
  }


  function formatDate(ns) {
    var date = new Date(parseInt(ns));
    var y = date.getFullYear();
    var m = date.getMonth() + 1;
    var d = date.getDate();

    if (m < 10) {
      m = '0' + m;
    }

    if (d < 10) {
      d = '0' + d;
    }

    return y + '-' + m + '-' + d;
  }
</script>

<style scoped>

  .container {
    height: 100vh;
    display: flex;
    flex-direction: column;
    align-items: center;
    padding: 20px 0;
    box-sizing: border-box;
  }

  @media screen and (min-width: 1281px) and (max-width: 2000px) {

  }


</style>
